<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
*   这里是微信的接口请求函数
*	@param $url 请求地址
*	@param $data 提交数据 
*/

if(!function_exists('https_request')){
	function https_request($url,$data=null){
	   $curl=curl_init();
       curl_setopt($curl,CURLOPT_URL,$url);
       curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
       curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
	   if(!empty($data)){
       curl_setopt($curl,CURLOPT_POST,1);
       curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
	   }
	   curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	   $output=curl_exec($curl);
       curl_close($curl);
       return $output;
	}
}


/**
* 模拟登陆mysise
* @param $url 请求地址
* @param $referer 来源地址，解决防盗链
* @param $cookies 就是cookies咯
* @param 模拟登陆时提交的数据
* @param $setheader   头部返回信息，主要是为了拿到cookie用的  
*/

if(!function_exists('verify_url')){

	function verify_url($url,$referer=null,$cookies=null,$data=null,$setheader=1){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		if(!empty($referer)){
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
		curl_setopt($ch, CURLOPT_HEADER, $setheader);
		if(!empty($data)){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
		}
		if(!empty($cookies)){
			curl_setopt($ch, CURLOPT_COOKIE, $cookies);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}


if(!function_exists('GetCookise'))
{
	function GetCookies($userid="1440112138",$password="14440111101331")
	{
		$origin = "http://class.sise.com.cn:7001/sise/login.jsp";
		$result = file_get_contents($origin);
		preg_match('/<input type="hidden" name="(.+?)"  value="(.+?)">/i', $result,$out);
		$hidden_key = $out[1];
		$hidden_value = $out[2];
		$post_url = "http://class.sise.com.cn:7001/sise/login_check.jsp";
		$post_data = array(
	    	$hidden_key => $hidden_value,
	    	"username"  => $userid,
	    	"password"	=> $password
	    );
	    $data = verify_url($post_url,$origin,null,$post_data,1);
	    if(!strpos($data, 'index.jsp')){
	    	$error = "error";
	    	return $error;
	    }
	   	preg_match('/JSESSIONID=.*;/i', $data, $match);
	    $cookies_str = substr($match[0], 0, strlen($match[0]) - 1);
	    return $cookies_str; 
	}
}

if(!function_exists('get_token')){
	function get_token($url){
		$json_info = json_decode(https_request($url),true);
		return $json_info;
	}
	
}

if(!function_exists('check_user')){
	function check_user($url,$sid,$password){
		$sData = array(
			'sid' => $sid,
			'password' => $password
			);
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $sData);
		$re = curl_exec($curl);
		curl_close($curl);
		return $re;

	}
}

?>
